﻿@model IEnumerable<WebUI.Models.MappingFileModel>
@{
    ViewBag.Title = "Citi Processes Dashboard";
}
<script type="text/javascript">
    $(function () {
        window.setInterval(search, 300000);

        $("#Processes, #Responsibles, #resultDate, #Status, #TodayProcess").change(search);

        $("#resultDate").datepicker({ 
            minDate: @System.Configuration.ConfigurationManager.AppSettings["FirstResultDate"], 
            maxDate: '0', 
            dateFormat: 'dd/mm/yy',
            altField: "#hdnResultDate",
            altFormat: "yymmdd"
        });
    });

    function search () {
        var location = '@Url.Action("Index", new { periodicity = string.IsNullOrEmpty(Request.QueryString["periodicity"]) ? " " : Request.QueryString["periodicity"] })';
        window.location = location + '&processName=' + $("#Processes :selected").val() 
                + '&responsible=' + $("#Responsibles :selected").val()
                + '&resultDate=' + $("#hdnResultDate").val()
                + '&todayProcess=' + $("#TodayProcess")[0].checked
                + '&status=' + $("#Status :selected").val();
    }
</script>
Date:
<input type="text" id="resultDate" style="width: 90px; margin-right: 30px;" 
    value="@string.Concat(ViewBag.ResultDate.Substring(6, 2), "/", ViewBag.ResultDate.Substring(4, 2), "/", ViewBag.ResultDate.Substring(0, 4))" />
<input type="hidden" id="hdnResultDate" value="@ViewBag.ResultDate" />
Process: @Html.DropDownList("ProcessSelected", (IEnumerable<SelectListItem>)ViewBag.Processes, new { style = "width:120px; margin-right:30px;", id = "Processes", name = "Processes" })
Responsible: @Html.DropDownList("ResponsibleSelected", (IEnumerable<SelectListItem>)ViewBag.Responsibles, new { style = "width:120px; margin-right:30px;", id = "Responsibles", name = "Responsibles" })
Status: @Html.DropDownList("StatusSelected", (IEnumerable<SelectListItem>)ViewBag.Status, new { style = "width:180px; margin-right:30px;", id = "Status", name = "Status" })
<div class="clear">
</div>
<br />
@Html.CheckBox("TodayProcess", (bool)ViewBag.TodayProcess, ViewBag.LastUpdate == null || DateTime.Today != ViewBag.LastUpdate.Date ? new { disabled = "disabled" } : null)
Only show process that must run today
<div class="rightAlign floatRight">
    Last Update: @(ViewBag.LastUpdate != null ? ViewBag.LastUpdate.ToString("yyyy-MM-dd HH:mm:ss") : "-")</div>
<br />
<br />
@if (Model != null && Model.Any())
{
    <table style="width: 100%; margin-bottom: 40px;">
        <tr>
            <th>
                Process
            </th>
            <th>
                Sub Process
            </th>
            <th>
                Responsible
            </th>
            <th>
                Periodicity
            </th>
            <th>
                Scheduled Finish Time
            </th>
            <th>
                Actual Finish Time
            </th>
            <th>
                Next Run At
            </th>
            <th>
                Status
            </th>
        </tr>
        @foreach (var item in Model)
        {       
            <tr>
                <td>
                    <center>@item.ProcessName</center>
                </td>
                <td>
                    @item.SubProcessName
                </td>
                <td class="center">
                    @item.Responsible
                </td>
                <td class="center">
                    @item.Periodicy
                </td>
                <td class="center">
                    @{
               if (item.DeadLine == DateTime.MinValue)
               {
                        @item.EndTime.ToShortTimeString()
               }
               else
               {
                   if (item.Periodicy == WebUI.Models.Periodicity.Daily.ToString())
                   {
                        @item.DeadLine.ToShortTimeString()
                   }
                   else
                   {   
                        @item.DeadLine.ToString("yyyy-MM-dd HH:mm");
                   }
               }
                    }
                </td>
                <td class="center">
                    @(item.ExecutedAt == DateTime.MinValue
                    ? "-"
                    : item.Periodicy == WebUI.Models.Periodicity.Daily.ToString()
                        ? item.ExecutedAt.ToShortTimeString()
                        : item.ExecutedAt.ToString("yyyy-MM-dd HH:mm"))
                </td>
                <td class="center">
                    @{
                var nextRunAt = item.ExecutedAt == DateTime.MinValue
                    ? item.DeadLine
                    : item.ExecutedAt.AddDays(item.GetDayByPeriodicity()).AddHours(-item.ExecutedAt.Hour + item.EndTime.Hour)
                        .AddMinutes(-item.ExecutedAt.Minute + item.EndTime.Minute);


                //if (item.Periodicy == WebUI.Models.Periodicity.Daily.ToString())
                //{
                //        @item.DeadLine.ToShortTimeString()
                //}
                //else
                //{   @item.DeadLine.ToString("yyyy-MM-dd HH:mm");
                //}
                        @nextRunAt.ToString("yyyy-MM-dd HH:mm");
                    }
                </td>
                <td class="center">
                    @if (item.Status == WebUI.Models.Status.Executed)
                    {
                        <img src="@Url.Content("~/Content/Images/bullet_green.png")" width="18px" alt="Executed" />
                    }
                    else if (item.Status == WebUI.Models.Status.Failed)
                    {
                        <img src="@Url.Content("~/Content/Images/bullet_red.png")" width="18px" alt="Failed" />
                    }
                    else if (item.Status == WebUI.Models.Status.Waiting)
                    {
                        <img src="@Url.Content("~/Content/Images/bullet_white.png")" width="18px" alt="Not Processed" />
                    }
                    else if (item.Status == WebUI.Models.Status.OnLate)
                    {
                        <img src="@Url.Content("~/Content/Images/bullet_Yellow.png")" width="18px" alt="Executed After Time" />
                    }
                    else
                    {
                        <img src="@Url.Content("~/Content/Images/bullet_Blue.png")" width="18px" alt="Executed (Not Validated)" />
                    }
                </td>
            </tr>
        }
    </table>
}
else
{
    <div style="margin-top: 20px;">
        There's no information for the selected filters.</div>
}